_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=8.3.0
pkgrel=1
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
url="https://www.octave.org"
license=('spdx:GPL-3.0-or-later')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-glpk")
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         $([[ ${CARCH} == i686 ]] && echo "${MINGW_PACKAGE_PREFIX}-suitesparse5" || echo "${MINGW_PACKAGE_PREFIX}-suitesparse")
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-sundials")
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-gnuplot")
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
checkdepends=("unzip"
              "zip")
optdepends=('texinfo: for help-support in Octave'
            "unzip: for decompressing .zip archives"
            "zip: for compressing .zip archives"
            "${MINGW_PACKAGE_PREFIX}-epstool: eps output with tight bounding box"
            "${MINGW_PACKAGE_PREFIX}-fltk: alternative plotting and dialogs"
            "${MINGW_PACKAGE_PREFIX}-gnuplot: alternative plotting"
            "${MINGW_PACKAGE_PREFIX}-portaudio: audio support")
source=(https://ftp.gnu.org/gnu/octave/octave-$pkgver.tar.xz{,.sig}
        "0001-no-namespace-in-extern-C.patch::https://hg.savannah.gnu.org/hgweb/octave/raw-rev/123c5d78fc9d"
        "0002-mk-doc-cache-path.patch"
        "0003-no-community-support.patch"
        "0005-makeinfo-perl.patch")
validpgpkeys=('DBD9C84E39FE1AAE99F04446B05F05B75D36644B')  # John W. Eaton
sha512sums=('b7c8724ca8376783e8563c2050c2d9219309e559116e5cba0fdc9f50780782ff4cc98d382bdbe26055aebffb6be99b110033c682f661a3966ea1b1ddb12d4ac3'
            'SKIP'
            'b3961b95e8eba47959f3d8dd5b7542c7578647d7a3fd79489ceeadc34444d69128011f6cb276f6cb1f09411e1253b6c6e2f1b8caa9db3fc2ada523f01682d5b1'
            '5742cde34ae9184276c830dab41dccb289991578656353658bf07315d89b1c3870c6c51520d5aae0cec8c7fb12c707a1970aa9dde353b4ab54e515d1d3cf66e0'
            'c737dc79352ebfec9651266c1853cb6cf6d75201019be0b4cc60bd12a1bde777a18b6e50e278f9980a6ffed96fb4442793f870b434c0c8abcbd521a2f4f53dd9'
            'c2796f1d83208595c4f52f27d14f3bedc64d95a514089108726b5a2de20296bdfb195f545e7045a322eebef4ce387b0f05547e071c74dfa5478f5689d0f207bd')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0002-mk-doc-cache-path.patch \
    0005-makeinfo-perl.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      0001-no-namespace-in-extern-C.patch \
      0003-no-community-support.patch

    # We need the patched libtool with Flang specific changes.
    autoreconf -fiv
  fi

}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=(# Clang seems to require a different set of dllexport
                    # attributes than GCC.  For the time being, export all
                    # symbols (including those from gnulib and other static
                    # libraries linked into the Octave libraries).  Doing that
                    # might cause issues in some cases.  But solving this
                    # without breaking GCC support might be difficult.
                    "--disable-lib-visibility-flags"
                    # Libtool still needs some hints how to handle LLVM Flang:
                    "--enable-fortran-calling-convention=gfortran"
                    "ac_cv_f77_compiler_gnu=yes"
                    "lt_cv_prog_gnu_ld=yes"
                    "F77=${MINGW_PREFIX}/bin/flang"
                    # LLVM 16 seems to have switched defaults to C++17.
                    # Apparently, some STL functions that are deprecated in that
                    # version of the standard are used.  For the time being,
                    # build with the C++14 feature set (and GNU extensions).
                    "CXX=${MINGW_PREFIX}/bin/clang++ -std=gnu++14")
    # The headers from some dependent libraries (graphicsmagick++?) cause a
    # torrent of warnings about deprecated declarations.  Sometimes to the
    # point that the build crashes with garbled characters in the terminal
    # output for some reason.  Just calling `make all -j2` again in the build
    # tree lets the compilation finish successfully.
    # Disable those warnings as a work-around.
    CXXFLAGS+=" -Wno-deprecated-declarations"
  fi

  # The configure step sometimes hangs for MINGW32 for currently unknown reasons.
  # Add a timeout of 30 minutes (well over what should be needed) to unblock
  # the build system in that case.
  timeout 30m \
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --enable-shared \
    --disable-static \
    --enable-relocate-all \
    --enable-link-all-dependencies \
    "${_extra_config[@]}" \
    gl_cv_have_weak=no \
    octave_cv_fftw3_threads_lib="-lfftw3_omp" \
    octave_cv_fftw3f_threads_lib="-lfftw3f_omp" \
    JAVA_HOME=""

  PERL5SHELL="bash -l -c" make all
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check || true
}

package() {
  make -C "build-${MSYSTEM}" DESTDIR="${pkgdir}" install

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
